﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WhoFeedFish
{
    class Program
    {
        static string[] Nationals = new String[] { "英国", "瑞典", "丹麦", "挪威", "德国" };
        static string[] Pets = new String[] { "狗", "鸟", "猫", "马", "鱼" };
        static string[] Cigarettes = new String[] { "Pall Mall", "Dunhill", "Blends", "Blue Master", "Prince" };
        static string[] Drinks = new String[] { "茶", "咖啡", "牛奶", "啤酒", "水" };
        static string[] Colors = new String[] { "红", "绿", "白", "蓝", "黄" };
        static int[] Locations = new int[] { 1, 2, 3, 4, 5 };

        static int counter = 0;
        static int s_counter = 0;

        static void Main(string[] args)
        {
            RunSnippet();

            Console.WriteLine("执行完毕..., 运行次数：" + counter.ToString() + ", 满足要求记录数：" + s_counter.ToString());

            Console.ReadKey();
        }

        public static void RunSnippet()
        {
            // Person[] Persons = new Person[]{new Person("英国"), new Person("瑞典"), new Person("丹麦"), new Person("挪威"), new Person("德国")};
            // House[] Houses = new House[]{new House("红"), new House("绿"), new House("白"), new House("蓝"), new House("黄")};

            Person[] Persons = new Person[] { new Person(), new Person(), new Person(), new Person(), new Person() };

            for (int i_na = 0; i_na < Nationals.Length; i_na++)
            {
                Set(Persons, i_na, "National");

                for (int i_col = 0; i_col < Colors.Length; i_col++)
                {
                    Set(Persons, i_col, "Color");

                    for (int i_pet = 0; i_pet < Pets.Length; i_pet++)
                    {
                        Set(Persons, i_pet, "Pet");

                        for (int i_ci = 0; i_ci < Cigarettes.Length; i_ci++)
                        {
                            Set(Persons, i_ci, "Cigarette");

                            for (int i_dr = 0; i_dr < Drinks.Length; i_dr++)
                            {
                                Set(Persons, i_dr, "Drink");

                                for (int i_loc = 0; i_loc < Locations.Length; i_loc++)
                                {
                                    Set(Persons, i_loc, "Location");
                                    counter++;

                                    if (CheckIsSatisfied2(Persons))
                                    {
                                        s_counter++;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private static void Set(Person[] Persons, int startPos, string propertyName)
        {
            int inner_i = startPos;

            for (int i_p = 0; i_p < Persons.Length; i_p++)
            {
                inner_i = (startPos + i_p) % 5;     // 5为数组长度

                switch (propertyName)
                {
                    case "National":
                        Persons[i_p].National = Nationals[inner_i];
                        break;
                    case "Pet":
                        Persons[i_p].Pet = Pets[inner_i];
                        break;
                    case "Cigarette":
                        Persons[i_p].Cigarette = Cigarettes[inner_i];
                        break;
                    case "Drink":
                        Persons[i_p].Drink = Drinks[inner_i];
                        break;
                    case "Color":
                        Persons[i_p].House.Color = Colors[inner_i];
                        break;
                    case "Location":
                        Persons[i_p].House.Location = Locations[inner_i];
                        break;
                }
            }
        }

        private static bool CheckIsSatisfied2(Person[] Persons)
        {
            Person p, p1, p2;

            // 条件1
            for (int i = 0; i < Persons.Length; i++)
            {
                p = Persons[i];

                if (p.National == "英国" && p.House.Color != "红" || p.National != "英国" && p.House.Color == "红") { return false; }
                if (p.National == "瑞典" && p.Pet != "狗" || p.National != "瑞典" && p.Pet == "狗") { return false; }
                if (p.National == "丹麦" && p.Drink != "茶" || p.National != "丹麦" && p.Drink == "茶") { return false; }
                // 4
                // if (p.House.Color == "绿" && p.Drink != "咖啡" || p.House.Color != "绿" && p.Drink == "咖啡") { return false; }
                if (p.Cigarette == "Pall Mall" && p.Pet != "鸟" || p.Cigarette != "Pall Mall" && p.Pet == "鸟") { return false; }
                // if (p.House.Color == "黄" && p.Cigarette != "Dunhill" || p.House.Color != "黄" && p.Cigarette == "Dunhill") { return false; }
                //if (p.House.Location == 3 && p.Drink != "牛奶" || p.House.Location != 3 && p.Drink == "牛奶") { return false; }
                if (p.National == "挪威" && p.House.Location != 1 || p.National != "挪威" && p.House.Location == 1) { return false; }
                // 10
                // 11
                if (p.Cigarette == "Blue Master" && p.Drink != "啤酒" || p.Cigarette != "Blue Master" && p.Drink == "啤酒") { return false; }
                // if (p.National == "德国" && p.Cigarette != "Prince" || p.National != "德国" && p.Cigarette == "Prince") { return false; }
                if (p.National == "挪威" && p.House.Color != "蓝" || p.National != "挪威" && p.House.Color == "蓝") { return false; }
                // 15
            }

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("--------------------------------------");

            // 条件1
            for (int i = 0; i < Persons.Length; i++)
            {
                Console.WriteLine(Persons[i].ToString());
            }

            Console.WriteLine("--------------------------------------");
            Console.WriteLine();
            Console.WriteLine();

            return true;
        }

        private static bool CheckIsSatisfied(Person[] Persons)
        {
            Person p, p1, p2;

            // 条件1
            for (int i = 0; i < Persons.Length; i++)
            {
                p = Persons[i];

                if (p.National == "英国" && p.House.Color != "红" || p.National != "英国" && p.House.Color == "红") { return false; }
                if (p.National == "瑞典" && p.Pet != "狗" || p.National != "瑞典" && p.Pet == "狗") { return false; }
                if (p.National == "丹麦" && p.Drink != "茶" || p.National != "丹麦" && p.Drink == "茶") { return false; }
                // 4
                if (p.House.Color == "绿" && p.Drink != "咖啡" || p.House.Color != "绿" && p.Drink == "咖啡") { return false; }
                if (p.Cigarette == "Pall Mall" && p.Pet != "鸟" || p.Cigarette != "Pall Mall" && p.Pet == "鸟") { return false; }
                if (p.House.Color == "黄" && p.Cigarette != "Dunhill" || p.House.Color != "黄" && p.Cigarette == "Dunhill") { return false; }
                // if (p.House.Location == 3 && p.Drink != "牛奶" || p.House.Location != 3 && p.Drink == "牛奶") { return false; }
                // if (p.National == "挪威" && p.House.Location != 1 || p.National != "挪威" && p.House.Location == 1) { return false; }
                // 10
                // 11
                if (p.Cigarette == "Blue Master" && p.Drink != "啤酒" || p.Cigarette != "Blue Master" && p.Drink == "啤酒") { return false; }
                if (p.National == "德国" && p.Cigarette != "Prince" || p.National != "德国" && p.Cigarette == "Prince") { return false; }
                if (p.National == "挪威" && p.House.Color != "蓝" || p.National != "挪威" && p.House.Color == "蓝") { return false; }
                // 15
            }
            /*
            // 条件2
            for (int i = 0; i < Persons.Length; i++)
            {
                p1 = Persons[i];

                for (int j = 0; j < Persons.Length; j++)
                {
                    p2 = Persons[j];

                    if (i != j)
                    {
                        if (p1.National == p2.National
                            || p1.Pet == p2.Pet
                            || p1.Cigarette == p2.Cigarette
                            || p1.Drink == p2.Drink
                            || p1.House.Color == p2.House.Color
                            || p1.House.Location == p2.House.Location
                            )
                        {
                            return false;
                        }

                        if (p1.House.Color == "绿" && p2.House.Color == "白")
                        {
                            if (p1.House.Location != p2.House.Location - 1)
                            {
                                return false;
                            }
                        }

                        if (p1.House.Color == "白" && p2.House.Color == "绿")
                        {
                            if (p1.House.Location != p2.House.Location + 1)
                            {
                                return false;
                            }
                        }

                        if (p1.Cigarette == "Blends" && p2.Pet == "猫")
                        {
                            if (p1.House.Location != p2.House.Location + 1 && p1.House.Location != p2.House.Location - 1)
                            {
                                return false;
                            }
                        }

                        if (p1.Cigarette == "Dunhill" && p2.Pet == "马")
                        {
                            if (p1.House.Location != p2.House.Location + 1 && p1.House.Location != p2.House.Location - 1)
                            {
                                return false;
                            }
                        }

                        if (p1.Cigarette == "Blends" && p2.Drink == "水")
                        {
                            if (p1.House.Location != p2.House.Location + 1 && p1.House.Location != p2.House.Location - 1)
                            {
                                return false;
                            }
                        }
                    }
                }
            }
             * */

            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("--------------------------------------");

            // 条件1
            for (int i = 0; i < Persons.Length; i++)
            {
                Console.WriteLine(Persons[i].ToString());
            }

            Console.WriteLine("--------------------------------------");
            Console.WriteLine();
            Console.WriteLine();

            return true;
        }
    }
}

public class Person
{
    public string National;
    public House House = new House();
    public string Pet;
    public string Cigarette;
    public string Drink;

    public override string ToString()
    {
        return String.Format("National: {0}, House Color: {1}, Location: {2}, Pet: {3}, Cigarette: {4}, Drink: {5}",
            this.National, this.House.Color, this.House.Location, this.Pet, this.Cigarette, this.Drink);
    }
}

public class House
{
    public House()
    {
    }

    public House(string color, int loc)
    {
        this.Color = color;
        this.Location = loc;
    }

    public string Color;
    public int Location;
}